import PyCmdMessenger
import wifisocket
import socket
import Socket_Main
import sys
import numpy as np

commands_earth = [
        ["kAcknowledge", "s"],  # Command to acknowledge that cmd was received,
        ["kErrorAlarm", "s"],  # Command to report errors
        ["kSetMuxBoxConnect", "ciiciiciicii"],
        # ���� switchbox ,arg= "A,Hb,Lb,B,Hb,Lb,M,Hb,Lb,N,Hb,Lb,", "C,00,00,C,00,00,C,00,00,"
        ["kSetTxWaveWithParam", "iiii"],
        # ����TX wave ����������,  char wavemode, char frequency, int16_t pulse_number , int16_t voltage
        ["kSartAdcSampleOnce", "s"],  # ����һ�β���,
        ["kCheckMaxVoltageForCurrent", "f"],  # ����Ԥ��ĵ�����Ҫ�ĵ�ѹ����, float , current value
        ["kReturnMaxVoltageForCurrent", "f"],  # ���صĵ�ѹֵ, float voltage
        ["kCheckPoleConnect", "iciicii"],  # ��缫�Ƿ���ȷ�� , ������ѹ���ú���AB������
        ["kReturnPoleConnectRes", "siff"],  # ��缫�Ƿ���ȷ�� , ����״̬��Ϣ���迹ֵ
        ["kEnableSaveLogInTFcard", "?"],  # ʹ����TF���ﱣ��log�ļ�?? */
        ["kAdcSampleLogging", "ii"],  # ADC �������� */
        ["kFindMuxBox", "c"],  # find box��Ҫ�ܳ�ʱ�䣬������Ҫ������ָ�������ؽ��
        ["kReportFindMuxBox", "iiiiiiiiii"],
        # ɨ���£�485������ �����Ӷ��ٸ�box�� ��Χ��1-10�� box����ʱ�� ��ַҲ��Ҫ����10 �� ����һ��10���ȵ�int���飬 ������Ҫ��ע��box�ڲ�������״̬�Ƿ������� ���Ǹ�ֵ��ʾ�����ַ���ҵ�box������box״̬���쳣
        ["kAskSystemInform", "s"],
        ["kReportSystemInform", "ff"],
        ["kClearAllMuxBoxConnect", "iiiiiiiiii"],
        # ÿ�������µ����ӣ���Ӧ��������е�box������״̬�������������ϵ����ӣ��γɶ�·������Ҫ�����box��ַ������ȥ
        ["kConfirm", "I"],  # log packet received confirm
        ["kReSync", "I"],  # when log send error, resync mcu and retry send
        ["kFirmwareVer", "s"],  # get HW firmware version
        ["kEnableSimulateMode", "c"],  # ����ģʽ�Ͳ���ģʽ
        ["kSetWifiAPNamepwd", "ss"],
        ["kResetAlarm", "s"]  # reset alarm
]
port_mode = "ip"
arduino = 0
ip= "192.168.4.1"

cmd = PyCmdMessenger.CmdMessenger(port_mode,arduino,commands_earth)

def onSendParamToMcu(modes, freqs, counts, voltages):
        # port_mode = "ip"
        # arduino = 0
        # cmd = PyCmdMessenger.CmdMessenger(port_mode,arduino,commands_earth)
        # cmd = _initSocket(ip)
        voltage = voltages
        freq = freqs
        count = counts
        mode_indx = modes
        if (mode_indx == 0):
                mode = 1  # ���ݲ�
        else:
                mode = 2  # ����

        # print("kSetTxWaveWithParam: vol: %d, freq: %d ,mode: %d, count: %d " % (
        #         str(voltage), str(freq), str(mode), str(count)))
        #print("kSetTxWaveWithParam: vol: "+str(voltage), "freq:"+str(freq) ,"mode:"+ str(mode), "count:" +str(count))
        data = cmd.send("kSetTxWaveWithParam", mode_indx, freq, count, voltage)
        #msg = cmd.receive()
        #print("----------------- onSendParamToMcu ------------- "+str(data))
        return data
def onKConfirmToMcu(val):

        print("kConfirmParam: vol: "+str(val))
        data = cmd.send("kConfirm", val)
        print(data)
        return data
def onKReSync(val):

        print("onKReSync: vol: "+str(val))
        data = cmd.send("kReSync", val)
        print(data)
        return data
def clear_mux(boxaddr1, boxaddr2, boxaddr3, boxaddr4,boxaddr5,boxaddr6, boxaddr7, boxaddr8, boxaddr9,boxaddr10):
        port_mode = "ip"
        arduino = 0
        cmd = PyCmdMessenger.CmdMessenger(port_mode,arduino,commands_earth)
        #self.c.reset_input_buffer(self.client_socket)
        cmd_name = ""
        #print("------------boxaddr1= "+str(boxaddr1)+"----boxaddr2= "+str(boxaddr2)+"---boxaddr3= "+str(boxaddr3)+"----boxaddr4= "+str(boxaddr4)+"----boxaddr5= "+str(boxaddr5))
        data = cmd.send("kClearAllMuxBoxConnect",boxaddr1,boxaddr2,boxaddr3,boxaddr4,boxaddr5,boxaddr6,boxaddr7,boxaddr8,boxaddr9,boxaddr10)
        #print("------------------------------clear_mux = "+str(data))
        return data
def update_poles_connect(a_pole,b_pole,m_pole,n_pole,model ):
        #self.SerialPortIsBusy = 1
        port_mode = "ip"
        arduino = 0
        cmd = PyCmdMessenger.CmdMessenger(port_mode,arduino,commands_earth)
        a_boxaddr = a_pole.split(",")[0]
        b_boxaddr = b_pole.split(",")[0]
        m_boxaddr = m_pole.split(",")[0]
        n_boxaddr = n_pole.split(",")[0]
        a_boxaddrval = a_pole.split(",")[1]
        b_boxaddrval = b_pole.split(",")[1]
        m_boxaddrval = m_pole.split(",")[1]
        n_boxaddrval = n_pole.split(",")[1]
        if model == 0 | model == 1:
                data = cmd.send("kSetMuxBoxConnect","A",int(a_boxaddr),int(a_boxaddrval),"B",int(b_boxaddr),int(b_boxaddrval),"M",int(m_boxaddr),int(m_boxaddrval),"N",int(n_boxaddr),int(n_boxaddrval))
        else:
                print("=====model===="+str(model))
                data = cmd.send("kSetMuxBoxConnect","A",int(a_boxaddr),int(a_boxaddrval),"M",int(m_boxaddr),int(m_boxaddrval),"N",int(n_boxaddr),int(n_boxaddrval),"B",int(b_boxaddr),int(b_boxaddrval))
        #print("------------------------------update_poles_connect = "+str(data)+" = "+a_boxaddrval+" = "+b_boxaddrval+" = "+m_boxaddrval+" = "+n_boxaddrval)
        return data

def subABMN(ABMN,a1_dtype,a1_shape,indx):
        # print(ABMN)
        # print(type(ABMN))
        abmn = ABMN.encode('ISO-8859-1')
        #print(abmn)
        ABMN1 = np.fromstring(abmn,dtype=getattr(np,a1_dtype)).reshape(eval(a1_shape))
        print(str(ABMN1[indx,:]))
        strd = ""
        for i in ABMN1[indx,:]:
                # print(i)
                strd += str(int(i))+","
        #print(strd)
        return strd.strip(",")
def clearAlarm():
        print("------------------ kResetAlarm start ---------------------")
        data = cmd.send("kResetAlarm","reset")
        print("------------------ kResetAlarm end ---------------------")
        print(data)
        return data
def CheckPoleConnect(voltage, a_res_boxaddr, a_res_pole, b_res_boxaddr,b_res_pole):
        print("------------------ kCheckPoleConnect start ---------------------")
        data = cmd.send("kCheckPoleConnect",voltage,"A",a_res_boxaddr,a_res_pole,"B",b_res_boxaddr,b_res_pole)
        print("------------------ kCheckPoleConnect end ---------------------")
        print(data)
        return data